

#ifndef LCD_INTERNAL_H__
#define LCD_INTERNAL_H__


#include "rtl876x.h"
#include "rtl876x_rcc.h"
#include "rtl876x_gpio.h"
#include "rtl876x_nvic.h"
#include "rtl876x_spi.h"
#include "rtl876x_pinmux.h"
#include "rtl876x_gdma.h"
#include "rtl876x_io_dlps.h"

#include "app_section.h"
#include "platform_utils.h"

#include "xgui_config.h"

#include "em_lcd.h"
#include "em_rtc.h"

#include "boards.h"

#include "log.h"


#define LCD_XFER_MAX_SIZE       4000

#define LCD_DELAY_MS    platform_delay_ms
#define LCD_OS_DELAY    os_delay

#define LCD_CS_LOW      Pad_Config( LCD_SPI_CS_PIN, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_DOWN, PAD_OUT_ENABLE, PAD_OUT_LOW )
#define LCD_CS_HIGH     Pad_Config( LCD_SPI_CS_PIN, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_DOWN, PAD_OUT_ENABLE, PAD_OUT_HIGH )

#define LCD_SET_RESET   Pad_Config( LCD_RESET_PIN, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_DOWN, PAD_OUT_ENABLE, PAD_OUT_LOW )
#define LCD_SET_nRESET  Pad_Config( LCD_RESET_PIN, PAD_SW_MODE, PAD_IS_PWRON, PAD_PULL_UP, PAD_OUT_ENABLE, PAD_OUT_HIGH )

#define LCD_RS_LOW      GPIO_ResetBits( GPIO_GetPin(LCD_RS_PIN) )
#define LCD_RS_HIGH     GPIO_SetBits( GPIO_GetPin(LCD_RS_PIN) )

#define LCD_SEND_DATA(data, len)        lcd_write_data(data, len)
#define LCD_SEND_DATA_ASYNC(data, len)  lcd_dma_send(data, len)

typedef struct 
{
    uint8_t*    pdata;      // 
    uint32_t    size;       // 
    uint32_t    offset;     // 
    
volatile    bool        isbusy;
volatile    bool        block_write;            // 
        
    struct
    {
        uint16_t    offset;    // 
        uint16_t    size;    // 
        uint16_t    count;   // 
        uint16_t    used;    // 
    }blocks;
}lcd_write_ctl_t;

extern volatile lcd_write_ctl_t lcd_wrctl;

//void lcd_config_gpio(void);

void lcd_common_init(void);

bool lcd_write_completed(void);

void lcd_write_command(uint8_t cmd);

void lcd_write_data(uint8_t*data, uint16_t len); 

//void em_lcd_write_data(uint8_t*data, uint16_t len);

void lcd_gdma_init(void); 

void lcd_dma_send(uint8_t* source_buf, uint32_t size);

void lcd_dma_send_it(uint8_t* source_buf, uint32_t size); 

void em_lcd_wait_transfer_done(void); 

void em_lcd_wait_transfer_it_set(void); 

void em_lcd_wait_transfer_idle(void);

/*****************************qspi interface*****************/
void lcd_qspi_write_cmd_para(uint16_t cmd , uint8_t *data , uint8_t data_len);

void lcd_qspi_enter_data_output_mode(void);

void lcd_qspi_clear(uint16_t xStart, uint16_t yStart, uint16_t xEnd, uint16_t yEnd, uint16_t color);
/*****************************qspi interface*****************/
#endif // 
